using System;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for SMK_EditListView.
	/// </summary>
	public class EditListView : ListView 
	{
		private ListViewItem li;
		private int X=0;
		private int Y=0;
		private string subItemText ;
		private int subItemSelected = 0 ; 
		private System.Windows.Forms.TextBox  editBox = new System.Windows.Forms.TextBox();
		private System.Windows.Forms.ComboBox cmbBox = new System.Windows.Forms.ComboBox();
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
        private System.Windows.Forms.ColumnHeader columnHeader5;
        private System.Windows.Forms.ColumnHeader columnHeader6;
        private System.Windows.Forms.ColumnHeader columnHeader7;
        private System.Windows.Forms.ColumnHeader columnHeader8;
        private System.Windows.Forms.ColumnHeader columnHeader9;
        private System.Windows.Forms.ColumnHeader columnHeader10;
        private System.Windows.Forms.ColumnHeader columnHeader11;
        private System.Windows.Forms.ColumnHeader columnHeader12;
        private System.Windows.Forms.ColumnHeader columnHeader13;
        private System.Windows.Forms.ColumnHeader columnHeader14;
        

		public EditListView()
		{
			cmbBox.Items.Add("Asia");
			cmbBox.Items.Add("North America");
			cmbBox.Items.Add("South America");
			cmbBox.Items.Add("Africa");
			cmbBox.Items.Add("Europe");
			cmbBox.Items.Add("Australia");
			cmbBox.Items.Add("Antartica");
			cmbBox.Size  = new System.Drawing.Size(0,0);
			cmbBox.Location = new System.Drawing.Point(0,0);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {this.cmbBox});			
			cmbBox.SelectedIndexChanged += new System.EventHandler(this.CmbSelected);
			cmbBox.LostFocus += new System.EventHandler(this.CmbFocusOver);
			cmbBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CmbKeyPress);
			cmbBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			cmbBox.BackColor = Color.SkyBlue; 
			cmbBox.DropDownStyle = ComboBoxStyle.DropDownList;
			cmbBox.Hide();


			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader9 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader10 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader11 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader12 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader13 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader14 = new System.Windows.Forms.ColumnHeader();
             

			this.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																			this.columnHeader1,
																			this.columnHeader2,
																			this.columnHeader3,
                                                                            this.columnHeader4,
																			this.columnHeader5,
																			this.columnHeader6,
                                                                            this.columnHeader7,
																			this.columnHeader8,
																			this.columnHeader9,
                                                                            this.columnHeader10,
																			this.columnHeader11,
																			this.columnHeader12,
                                                                            this.columnHeader13,
																			this.columnHeader14});
			this.Dock = System.Windows.Forms.DockStyle.Fill;
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FullRowSelect = true;
			this.Name = "listView1";
			this.Size = new System.Drawing.Size(0,0);
			this.TabIndex = 0;
			this.View = System.Windows.Forms.View.Details;
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SMKMouseDown);
			this.DoubleClick += new System.EventHandler(this.SMKDoubleClick);
			this.GridLines = true ;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Desc";
			this.columnHeader1.Width = 100;
			// 
			// columnHeader2
			// 
            this.columnHeader2.Text = "OrgMeasurement";
			this.columnHeader2.Width = 100;
			// 
			// columnHeader3
			// 
            this.columnHeader3.Text = "CrtMeasurement";
			this.columnHeader3.Width = 100;
			// 
			// columnHeader4
			// 
            this.columnHeader4.Text = "Note";
			this.columnHeader4.Width = 100;

            this.columnHeader5.Text = "Unit";
            this.columnHeader5.Width = 100;
            // 
            // columnHeader2
            // 
            this.columnHeader6.Text = "Layer";
            this.columnHeader6.Width = 100;
            // 
            // columnHeader3
            // 
            this.columnHeader7.Text = "Coverage";
            this.columnHeader7.Width = 100;
            // 
            // columnHeader4
            // 
            this.columnHeader8.Text = "Waste";
            this.columnHeader8.Width = 100;
            this.columnHeader9.Text = "PurUnit";
            this.columnHeader9.Width = 100;
            // 
            // columnHeader2
            // 
            this.columnHeader10.Text = "PurUnit Type";
            this.columnHeader10.Width = 100;
            // 
            // columnHeader3
            // 
            this.columnHeader11.Text = "Cost Per PurUnit";
            this.columnHeader11.Width = 100;
            // 
            // columnHeader4
            // 
            this.columnHeader12.Text = "Material";
            this.columnHeader12.Width = 100;
            this.columnHeader13.Text = "Labour";
            this.columnHeader13.Width = 100;
            // 
            // columnHeader2
            // 
            this.columnHeader14.Text = "Equipment";
            this.columnHeader14.Width = 100;
            // 
            // columnHeader3
            // 
             

			editBox.Size  = new System.Drawing.Size(0,0);
			editBox.Location = new System.Drawing.Point(0,0);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {this.editBox});			
			editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);
			editBox.LostFocus += new System.EventHandler(this.FocusOver);
			editBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			editBox.BackColor = Color.LightYellow; 
			editBox.BorderStyle = BorderStyle.Fixed3D;
			editBox.Hide();
			editBox.Text = "";

           
		}

		private void CmbKeyPress(object sender , System.Windows.Forms.KeyPressEventArgs e)
		{
			if ( e.KeyChar == 13 || e.KeyChar == 27 )
			{
				cmbBox.Hide();
			}
		}

		private void CmbSelected(object sender , System.EventArgs e)
		{
			int sel = cmbBox.SelectedIndex;
			if ( sel >= 0 )
			{
				string itemSel = cmbBox.Items[sel].ToString();
				li.SubItems[subItemSelected].Text = itemSel;
			}
		}

		private void CmbFocusOver(object sender , System.EventArgs e)
		{
			cmbBox.Hide() ;
		}
	
		private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if ( e.KeyChar == 13 ) 
			{
				li.SubItems[subItemSelected].Text = editBox.Text;
				editBox.Hide();
			}

			if ( e.KeyChar == 27 ) 
				editBox.Hide();
		}

		private void FocusOver(object sender, System.EventArgs e)
		{
			li.SubItems[subItemSelected].Text = editBox.Text;
			editBox.Hide();
		}

		public  void SMKDoubleClick(object sender, System.EventArgs e)
		{
			// Check the subitem clicked .
            try
            {
                int nStart = X;
                int spos = 0;
                int epos = this.Columns[0].Width;
                for (int i = 0; i < this.Columns.Count; i++)
                {
                    if (nStart > spos && nStart < epos)
                    {
                        subItemSelected = i;
                        break;
                    }

                    spos = epos;
                    epos += this.Columns[i].Width;
                }

               // Console.WriteLine("SUB ITEM SELECTED = " + li.SubItems[subItemSelected].Text);
                subItemText = li.SubItems[subItemSelected].Text;

                string colName = this.Columns[subItemSelected].Text;
                if (colName == "Name")
                {
                    Rectangle r = new Rectangle(spos, li.Bounds.Y, epos, li.Bounds.Bottom);
                    cmbBox.Size = new System.Drawing.Size(epos - spos, li.Bounds.Bottom - li.Bounds.Top);
                    cmbBox.Location = new System.Drawing.Point(spos, li.Bounds.Y);
                    cmbBox.Show();
                    cmbBox.Text = subItemText;
                    cmbBox.SelectAll();
                    cmbBox.Focus();
                }
                else
                {
                    Rectangle r = new Rectangle(spos, li.Bounds.Y, epos, li.Bounds.Bottom);
                    editBox.Size = new System.Drawing.Size(epos - spos, li.Bounds.Bottom - li.Bounds.Top);
                    editBox.Location = new System.Drawing.Point(spos, li.Bounds.Y);
                    editBox.Show();
                    editBox.Text = subItemText;
                    editBox.SelectAll();
                    editBox.Focus();
                }
            }
            catch
            {
            }
		}

		public void SMKMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			li = this.GetItemAt(e.X , e.Y);
			X = e.X ;
			Y = e.Y ;
		}

	}
}
